﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QuerySQuoteByOrder : BasePage
    {
        int m_count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdOrderID.Value = UIHelper.GetQueryStringValue("orderid");
                BindData(0);
            }
        }

        private void BindData(int pageIndex)
        {
            string strQuoteID = this.txtQuoteID.Text.Trim();
            string strProductName = this.txtProductName.Text.Trim();
            DataTable dt = BsQuoteBusiness.SelectDialogList(strQuoteID, strProductName, hdOrderID.Value.Trim(), pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            dt.Columns.Add("IsTaxName");
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string isTax = CommonMethod.GetString(dr["IsTax"]);
                    if (!string.IsNullOrEmpty(isTax) && isTax == "1")
                    {
                        dr["IsTaxName"] = "是";
                    }
                    else
                    {
                        dr["IsTaxName"] = "否";
                    }
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string sReturn = "";
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                Label lblQuoteID = gvr.FindControl("lblQuoteID") as Label;
                Label lblCustomerID = gvr.FindControl("lblCustomerID") as Label;
                Label lblCustomerName = gvr.FindControl("lblCustomerName") as Label;
                Label lblContactName = gvr.FindControl("lblContactName") as Label;
                Label lblMobile = gvr.FindControl("lblMobile") as Label;
                Label lblSendCustomerID = gvr.FindControl("lblSendCustomerID") as Label;
                Label lblSendCustomerName = gvr.FindControl("lblSendCustomerName") as Label;
                Label lblSendAddr = gvr.FindControl("lblSendAddr") as Label;
                Label lblSendContactName = gvr.FindControl("lblSendContactName") as Label;
                Label lblSendMobile = gvr.FindControl("lblSendMobile") as Label;
                Label lblCurrency = gvr.FindControl("lblCurrency") as Label;
                Label lblIsTax = gvr.FindControl("lblIsTax") as Label;
                Label lblTaxRate = gvr.FindControl("lblTaxRate") as Label;

                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    if (((RadioButton)gvr.FindControl("rbSelect")).Checked)
                    {
                        sReturn = HttpUtility.UrlEncode(lblQuoteID.Text.Trim() + "," + 
                            lblCustomerID.Text.Trim() + "," +
                            lblCustomerName.Text.Trim() + "," +
                            lblContactName.Text.Trim() + "," +
                            lblMobile.Text.Trim() + "," +
                            lblSendCustomerID.Text.Trim() + "," +
                            lblSendCustomerName.Text.Trim() + "," +
                            lblSendAddr.Text.Trim() + "," +
                            lblSendContactName.Text.Trim() + "," +
                            lblSendMobile.Text.Trim() + "," +
                            lblCurrency.Text.Trim() + "," +
                            lblIsTax.Text.Trim() + "," +
                            lblTaxRate.Text.Trim() , Encoding.UTF8);
                        break;
                    }
                }
            }

            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "');CloseTopDialog();</script>");
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {

            BindData(0);
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("rbSelect");
                rb.Attributes.Add("onclick", "judge(this)");
            }
        }
    }
}